package com.coollang.smartbra.fragment;

import android.support.annotation.IdRes;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.coollang.smartbra.R;
import com.coollang.smartbra.activity.Welcome2Activity;
import com.coollang.smartbra.base.BaseFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @Auther: Timmy
 * @Data: 2017/7/19
 * @Fuction:
 */

public class GoalFragment extends BaseFragment {

    @BindView(R.id.rb_item_0)
    RadioButton mRbItem0;
    @BindView(R.id.rb_item_1)
    RadioButton mRbItem1;
    @BindView(R.id.rb_item_2)
    RadioButton mRbItem2;
    @BindView(R.id.rb_item_3)
    RadioButton mRbItem3;
    @BindView(R.id.radio_group)
    RadioGroup mRadioGroup;

    @BindView(R.id.tv_finish)
    TextView mTvFinish;

    Unbinder unbinder;
    private int mGoal;

    @Override
    protected View inflateView(LayoutInflater inflater) {
        View inflate = inflater.inflate(R.layout.item_difficulty, null, false);
        unbinder = ButterKnife.bind(this, inflate);
        return inflate;
    }

    @Override
    protected void initView(View view) {
        initRadioButtonTv();
    }

    private void initRadioButtonTv() {
        // 设置 TextView 的字体
        mRbItem0.setText(setBatteryCount(mRbItem0.getText().toString()));
        mRbItem1.setText(setBatteryCount(mRbItem1.getText().toString()));
        mRbItem2.setText(setBatteryCount(mRbItem2.getText().toString()));
        mRbItem3.setText(setBatteryCount(mRbItem3.getText().toString()));


        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                mTvFinish.setVisibility(View.VISIBLE);
                getCheckDifficuty(checkedId);
            }
        });
    }

    private SpannableString setBatteryCount(String text) {
        SpannableString msp = new SpannableString(text);        // 第一行
        msp.setSpan(new RelativeSizeSpan(0.625f), 2, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return msp;
    }

    private void getCheckDifficuty(int checkedId) {
        switch (checkedId) {
            case R.id.rb_item_0:
                mGoal = 1;
                break;
            case R.id.rb_item_1:
                mGoal = 2;
                break;
            case R.id.rb_item_2:
                mGoal = 3;
                break;
            case R.id.rb_item_3:
                mGoal = 4;
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tv_finish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_finish:
                // 请求网络连接
                ((Welcome2Activity)getActivity()).requestGoal(mGoal);
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
